using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Drawing;

namespace Weather
{
    public class RegisterForm : Form
    {
        private TextBox txtUsername;
        private TextBox txtPassword;
        private TextBox txtConfirmPassword;
        private Button btnRegister;
        private Label lblTitle;

        public RegisterForm()
        {
            InitializeComponents();
        }

        private void InitializeComponents()
        {
            this.Size = new Size(400, 350);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "注册";
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;

            // 标题
            lblTitle = new Label
            {
                Text = "用户注册",
                Location = new Point(0, 30),
                Size = new Size(400, 40),
                TextAlign = ContentAlignment.MiddleCenter,
                Font = new Font("Microsoft YaHei", 18, FontStyle.Bold)
            };

            // 用户名输入框
            txtUsername = new TextBox
            {
                Location = new Point(100, 100),
                Size = new Size(200, 30),
                PlaceholderText = "请输入用户名"
            };

            // 密码输入框
            txtPassword = new TextBox
            {
                Location = new Point(100, 140),
                Size = new Size(200, 30),
                PasswordChar = '•',
                PlaceholderText = "请输入密码"
            };

            // 确认密码输入框
            txtConfirmPassword = new TextBox
            {
                Location = new Point(100, 180),
                Size = new Size(200, 30),
                PasswordChar = '•',
                PlaceholderText = "请确认密码"
            };

            // 注册按钮
            btnRegister = new Button
            {
                Text = "注册",
                Location = new Point(100, 220),
                Size = new Size(200, 30),
                BackColor = Color.FromArgb(92, 184, 92),
                ForeColor = Color.White,
                FlatStyle = FlatStyle.Flat
            };
            btnRegister.Click += BtnRegister_Click;

            this.Controls.AddRange(new Control[] { 
                lblTitle, txtUsername, txtPassword, txtConfirmPassword, btnRegister 
            });
        }

        private void BtnRegister_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUsername.Text) || 
                string.IsNullOrEmpty(txtPassword.Text) || 
                string.IsNullOrEmpty(txtConfirmPassword.Text))
            {
                MessageBox.Show("请填写所有信息！");
                return;
            }

            if (txtPassword.Text != txtConfirmPassword.Text)
            {
                MessageBox.Show("两次输入的密码不一致！");
                return;
            }

            try
            {
                // 检查用户名是否已存在
                string checkSql = "SELECT COUNT(*) FROM users WHERE username = @username";
                var checkParam = new MySqlParameter("@username", MySqlDbType.VarChar) { Value = txtUsername.Text };
                var count = Convert.ToInt32(DBHelper.ExecuteScalar(checkSql, checkParam));

                if (count > 0)
                {
                    MessageBox.Show("用户名已存在！");
                    return;
                }

                // 插入新用户
                string sql = "INSERT INTO users (username, password) VALUES (@username, @password)";
                var parameters = new MySqlParameter[]
                {
                    new MySqlParameter("@username", MySqlDbType.VarChar) { Value = txtUsername.Text },
                    new MySqlParameter("@password", MySqlDbType.VarChar) { Value = txtPassword.Text }
                };

                DBHelper.ExecuteNonQuery(sql, parameters);
                MessageBox.Show("注册成功！");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"注册失败：{ex.Message}");
            }
        }
    }
} 